<?php
include_once('config/config.php');
include_once('./config/logout.php');

$auth = check_auth();
// 当前页
$page = $_GET['page'] ?? 1;

// 每页显示多少条数据
$limit = 3;

// 页码
$size = 5;

// 获取偏移量
$start = ($page - 1) * $limit;

// 获取数据
$adminSql = "SELECT `id`,`username` , `avatar` FROM `pre_admin` ORDER BY `id` DESC LIMIT $start,$limit";

// 获取数据总数
$countSql = "SELECT COUNT(*) FROM `pre_admin`";

$count = find($countSql);
// var_dump($count);
// exit;
// 查询
$adminData = finds($adminSql);
// var_dump($count);
// var_dump($personData);
$showPage = showPage($page, $count['COUNT(*)'], $limit);
// var_dump($showPage);
// exit;
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <?php include_once('common/meta.php') ?>
</head>

<body>

    <?php include_once('common/header.php'); ?>

    <?php include_once('common/menu.php'); ?>
    <div class="content">
        <div class="header">
            <h1 class="page-title">管理员列表</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.php">Home</a> <span class="divider">/</span></li>
            <li class="active">Index</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='AdminAdd.php'"><i class="icon-plus"></i>添加管理员</button>
                </div>
                <div class="well">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>用户名</th>
                                <th>头像</th>
                                <th style="width: 26px;"></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($adminData as $item) { ?>
                                <tr>
                                    <td><?php echo $item['id']; ?></td>
                                    <td>
                                        <a href="<?php echo is_file('.' . $item['avatar']) ? $item['avatar'] : '/assets/images/avatar.png' ?>" target="_blank">
                                            <img src="<?php echo is_file('.' . $item['avatar']) ? $item['avatar'] : '/assets/images/firstavatar.png' ?>" alt="头像" width="35">
                                        </a>
                                    </td>
                                    <td><?php echo $item['username']; ?></td>

                                    <td>
                                        <a href="adminEdit.php?id=<?php echo $item['id']; ?>"><i class="icon-pencil"></i></a>
                                        <a href="#myModal" data-id="<?php echo $item['id']; ?>" role="button" data-toggle="modal"><i class="icon-remove"></i></a>
                                    </td>
                                </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
                <div class="pagination">
                    <ul>
                        <?php echo $showPage; ?>
                    </ul>
                </div>

                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">删除管理员</h3>
                    </div>
                    <div class="modal-body">
                        <p class="error-text"><i class="icon-warning-sign modal-icon"></i>是否确认删除管理员？</p>
                    </div>
                    <div class="modal-footer">
                        <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
                        <button class="btn btn-danger delete" data-dismiss="modal">删除</button>
                    </div>
                </div>

                <?php include_once('common/footer.php'); ?>
            </div>
        </div>
    </div>
</body>

</html>
<?php include_once('common/script.php'); ?>
<script>
        // 全局管理员id
        let id = null;

$('a[href="#myModal"]').click(function(){
    // 获取类似data-dismiss以data为前缀的值
    id = $(this).data('id');// js dateset
});

$('.delete').click(function(){
    $.ajax({
        type:'post',
        url:'api.php',
        data:{
            id,
            action:'admin_del'
        },
        dataType:'json',
        success:function(res)
        {
            if(res && res.code === 1)
            {
                alert(res.msg);
                location.reload();
                return;
            }else{
                alert(res.msg);
                return;
            }
        }
    })
});

</script>